code source game Bi-a 3D

23.684 lượt xem;
1 using System.Collections.Generic;
2 using
UnityEngine;
3
4 namespace
ThreeDPool.States
5 {
6     
public class FSM : MonoBehaviour
7     {
8         
private List<IState> _states;
9
10         
private IState _currentState;
11
12         
public void AddState(IState state)
13         {
14             
// only add if this state is not
15             
if(_states.Find(s => s.GetType() == state.GetType()) != null)
16                 _states.Add(state);
17         }
18
19         
public void ChangeStateTo(IState newState)
20         {
21             
// dont change the state if in that state already
22             
if (newState == _currentState)
23                 
return;
24
25             
// exit from the current state if there is any
26             
if(_currentState != null)
27                 _currentState.OnExit();
28             
29             
// enter the new state
30             
if(newState != null)
31             {
32                 _currentState = newState;
33                 _currentState.OnEnter();
34             }
35         }
36
37         
public void Update()
38         {
39             
// update the current state
40             
if (_currentState != null)
41                 _currentState.OnUpdate();
42         }
43     }
44 }


Gõ tìm kiếm nhanh...